export async function chatCompletions(userInput: string) {
  console.log("🚀 ~ chatCompletions ~ userInput:", userInput);
  // 调用一个第三方的http请求
  // const res = await fetch("https://api.openai.com/v1/chat/completions", {
  //   method: "POST",
  //   headers: {
  //     "Content-Type": "application/json",
  //     Authorization: "Bearer sk-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
  //   },
  //   body: JSON.stringify({
  //     model: "gpt-3.5-turbo",
  //   }),
  // });

  const reqBody = {
    model: "max-32k",
    user: "用户唯一id",
    messages: [
      { role: "system", content: "你是知识渊博的助理" },
      { role: "user", content: userInput },
    ],
    temperature: 0.5,
    top_k: 4,
    stream: false,
    max_tokens: 1024,
    presence_penalty: 1,
    frequency_penalty: 1,
    tools: [{ type: "web_search", web_search: { enable: true } }],
    suppress_plugin: ["knowledge"],
  };

  const options = {
    method: "POST",
    headers: {
      "Content-Type": "application/json",
      // TODO：从哪里来的？
      Authorization: "Bearer ZXwnCCzsyIhWPtKbrDbN:CjAsEtTCDYjDokXFdZeb",
      Accept: "*/*",
      "Accept-Encoding": "gzip, deflate, br",
      "User-Agent": "PostmanRuntime-ApipostRuntime/1.1.0",
      Connection: "keep-alive",
    },
    body: JSON.stringify(reqBody),
  };

  try {
    const response = await fetch(
      "https://spark-api-open.xf-yun.com/v1/chat/completions",
      options
    );
    const responseJson = await response.json();
    if (responseJson.code === 0) {
      try {
        return responseJson.data.choices[0].text;
      } catch (error) {
        return responseJson.choices[0].message.content;
      }
    }
    return responseJson;
  } catch (err) {
    console.log("🚀 ~ chatCompletions ~ err:", err);
    return err;
  }
}
